{% extends "base.html" %}

{% block title %}价格统计 - 惠农平台{% endblock %}

{% block content %}
<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-header">
                <h3><i class="fas fa-chart-bar me-2"></i>不同类型农产品价格统计</h3>
            </div>
            <div class="card-body">
                <!-- 筛选条件 -->
                <div class="row mb-4">
                    <div class="col-md-4">
                        <label for="statTypeSelect" class="form-label">统计类型</label>
                        <select class="form-select" id="statTypeSelect">
                            <option value="avg" selected>平均价格</option>
                            <option value="min">最低价格</option>
                            <option value="max">最高价格</option>
                        </select>
                    </div>
                    <div class="col-md-4 d-flex align-items-end">
                        <button type="button" class="btn btn-primary" onclick="updatePriceStatistics()">
                            <i class="fas fa-refresh me-1"></i>刷新图表
                        </button>
                    </div>
                </div>

                <!-- 图表容器 -->
                <div class="chart-container">
                    <div id="priceStatisticsChart" style="width: 100%; height: 500px;"></div>
                </div>
                <!-- 统计说明卡片 -->
                <div class="row mt-4" id="chartDescription"></div>

                <!-- 调试信息 -->
                <div id="debugInfo" class="mt-4 d-none">
                    <div class="alert alert-info">
                        <h5><i class="fas fa-bug me-2"></i>调试信息</h5>
                        <pre id="debugData" class="mb-0"></pre>
                    </div>
                </div>

                <!-- 统计信息卡片 -->
                <div class="row mt-4" id="statsCards">
                    <!-- 卡片将通过JavaScript动态生成 -->
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
$(document).ready(function() {
    $('#refresh-chart').on('click', function() {
        fetchChartData();
    });

    function fetchChartData() {
        const type = $('#stat-type').val();
        const chartContainer = $('#chart-container');
        const errorContainer = $('#error-container');
        const loadingSpinner = $('#loading-spinner');

        loadingSpinner.show(); // 显示加载动画
        chartContainer.hide();
        errorContainer.hide();

        $.ajax({
            url: '{{ url_for("api_price_statistics") }}',
            data: { type: type },
            success: function(res) {
                if (res.code === 200 && res.chart) {
                    const chart = JSON.parse(res.chart);
                    Plotly.newPlot('chart-container', chart.data, chart.layout);
                    chartContainer.show();
                } else {
                    errorContainer.text(res.message || '加载图表失败').show();
                }
            },
            error: function() {
                errorContainer.text('请求图表数据时发生错误').show();
            },
            // complete 回调函数会在请求完成后执行，无论成功或失败
            complete: function() {
                loadingSpinner.hide(); // 隐藏加载动画
            }
        });
    }

    // 页面加载时自动加载一次图表
    fetchChartData();
});
</script>
{% endblock %}